document.addEventListener("DOMContentLoaded", function () {
  document
    .getElementById("blog-list")
    .addEventListener("click", function (event) {
      // 检查点击事件的目标是否是删除图标项
      if (event.target.tagName === "IMG") {
        let btnKey = event.target.getAttribute("data-key");
        if (btnKey === "del-blog") {
          var blog_id = event.target.getAttribute("data-id"); // 获取data-id属性
          var title = event.target.getAttribute("data-name"); // 获取data-name属性
          console.log("blog_id:", blog_id, "title:", title); // 输出数据
          let showText = "确定删除【" + title + "】此条博客吗？";
          if (confirm(showText)) {
            let csrfmiddlewaretoken = $(
              "input[name='csrfmiddlewaretoken']"
            ).val();
            $.ajax("/myblog/delete", {
              method: "POST",
              data: { blog_id, csrfmiddlewaretoken },
              success: function (res) {
                if (res["code"] == 200) {
                  window.location = "/myblog/index";
                } else {
                  alert("博客删除失败！");
                }
              },
              fail: function (err) {
                console.log(err);
                alert("请求失败,博客删除失败！");
              },
            });
          }
        }
      }
    });
});
